<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $user_id
 * @property string $user_name
 * @property string $user_login
 * @property string $user_password
 * @property string $user_email
 * @property integer $user_role
 * @property string $user_create_time
 * @property string $user_edit_time
 * @property string $verifyPassword
 */
class User extends CActiveRecord
{
	const ROLE_ADMIN=0;
	const ROLE_TEACHER=1;
	const ROLE_STUDENT=2;
	
	public $verifyPassword;
	
	public $verifyCode;
	
	private $_oldPsw;
	
	function getRoleName(){
		return array(
			self::ROLE_ADMIN =>'管理员',
			self::ROLE_TEACHER =>'老师',
			self::ROLE_STUDENT =>'学生',
		);
	}
	function getUserRole(){
		return array(
			//self::ROLE_ADMIN =>'ADMIN',
			self::ROLE_TEACHER =>'老师',
			self::ROLE_STUDENT =>'学生',
		);
	}
	function getRole(){
		$roleOption = $this->RoleName;
		return isset($roleOption[$this->user_role])?$roleOption[$this->user_role]:'未知角色';
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}
	/**
	 * @return the $user_id
	 */
	public function getId() {
		return $this->user_id;
	}

	/**
	 * @param $user_id the $user_id to set
	 */
	public function setId($user_id) {
		$this->user_id = $user_id;
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_name,user_login,user_role','required'),
			array('user_password,verifyPassword,verifyCode','required','on'=>'reg'),
			array('verifyPassword', 'compare', 'compareAttribute'=>'user_password', 'message' => '重复密码不匹配','on'=>'reg'),
			array('user_role', 'in', 'range'=>array(0,1,2)),
			array('user_login,user_name,user_email', 'unique'),
			array('user_email', 'email'),
			array('user_name, user_login', 'length', 'min' => 3,'max'=>20),
			array('user_password', 'length', 'min' => 3,'max'=>40),
			array('user_email', 'length', 'max'=>255),
			array('user_login', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => '只能为数字或者字母'),
			array('user_name,user_login,user_password,user_role,user_email', 'safe'),
			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd'),'on'=>'reg'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_name, user_login, user_password, user_email, user_role, user_create_time, user_edit_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_name' => '姓名',
			'user_login' => '帐号',
			'user_password' => '密码',
			'verifyPassword' => '重复密码',
			'user_email' => '邮箱',
			'user_role' => '身份',
			'user_create_time' => '创建时间',
			'user_edit_time' => '修改时间',
			'verifyCode'=>'验证码',
		);
	}
	protected function afterFind()
	{
		parent::afterFind();
		$this->_oldPsw=$this->user_password;
	}
	
	/**
	 * 系统填写数值
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->user_create_time=$this->user_edit_time=date("Y-m-d H:i:s");
				$this->user_password = md5($this->user_password);
			}
			else{
				if($this->user_password)
				{
					$this->user_password = md5($this->user_password);
				}else
				{
					$this->user_password = $this->_oldPsw;
				}
				$this->user_edit_time=date("Y-m-d H:i:s");
			}
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);

		$criteria->compare('user_name',$this->user_name,true);

		$criteria->compare('user_login',$this->user_login,true);

		$criteria->compare('user_password',$this->user_password,true);

		$criteria->compare('user_email',$this->user_email,true);

		$criteria->compare('user_role',$this->user_role);

		$criteria->compare('user_create_time',$this->user_create_time,true);

		$criteria->compare('user_edit_time',$this->user_edit_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}
